package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.base.UserContextInfo;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.entity.ApCookbook;
import com.mars.module.admin.mapper.ApCookbookMapper;
import com.mars.module.system.entity.BaseEntity;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.ApUserBrowseRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.ApUserBrowseMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.ApUserBrowse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.IApUserBrowseService;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 浏览记录业务层处理
 *
 * @author mars
 * @date 2024-03-05
 */
@Slf4j
@Service
@AllArgsConstructor
public class ApUserBrowseServiceImpl implements IApUserBrowseService {

    private final ApUserBrowseMapper baseMapper;

    private final ApCookbookMapper apCookbookMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ApUserBrowseRequest request) {
        ApUserBrowse apUserBrowse = baseMapper.selectOne(Wrappers.lambdaQuery(ApUserBrowse.class).eq(ApUserBrowse::getFoodId, request.getFoodId()).eq(ApUserBrowse::getUserId, request.getUserId()));
        if (Objects.isNull(apUserBrowse)) {
            ApUserBrowse entity = ApUserBrowse.builder().build();
            BeanUtils.copyProperties(request, entity);
            baseMapper.insert(entity);
        } else {
            apUserBrowse.setUpdateTime(LocalDateTime.now());
            baseMapper.updateById(apUserBrowse);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(ApUserBrowseRequest request) {
        ApUserBrowse entity = ApUserBrowse.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public ApUserBrowse getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<ApUserBrowse> pageList(ApUserBrowseRequest request) {
        Page<ApUserBrowse> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<ApUserBrowse> query = this.buildWrapper(request);
        IPage<ApUserBrowse> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<ApUserBrowse> list(ApUserBrowseRequest request) {
        LambdaQueryWrapper<ApUserBrowse> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    @Override
    public List<ApCookbook> browseRecord(ApUserBrowseRequest apUserBrowseRequest) {
        UserContextInfo info = ContextUserInfoThreadHolder.get();
        Long userId = info.getId();
        List<ApUserBrowse> apUserBrowses = baseMapper.selectList(Wrappers.lambdaQuery(ApUserBrowse.class)
                .eq(ApUserBrowse::getUserId, userId).orderByDesc(BaseEntity::getUpdateTime));
        if (CollectionUtils.isEmpty(apUserBrowses)) {
            return new ArrayList<>();
        }
        List<Long> foodIds = apUserBrowses.stream().map(ApUserBrowse::getFoodId).collect(Collectors.toList());
        return apCookbookMapper.selectList(Wrappers.lambdaQuery(ApCookbook.class).in(ApCookbook::getFoodId, foodIds));
    }

    private LambdaQueryWrapper<ApUserBrowse> buildWrapper(ApUserBrowseRequest param) {
        LambdaQueryWrapper<ApUserBrowse> query = new LambdaQueryWrapper<>();
        return query;
    }

}
